﻿Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class admin_categories
        Inherits ActiveAdminBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            BindGroups()
        End Sub

        Private Sub agCategories_Callback(ByVal sender As Object, ByVal e As Controls.CallBackEventArgs) Handles agCategories.Callback
            Try
                If Not e.Parameters(4) = "" Then
                    Dim sAction As String = e.Parameters(4).Split(CChar(":"))(0)

                    Select Case sAction.ToUpper
                        Case "DELETE"
                            Dim TagId As Integer = CInt(e.Parameters(4).Split(CChar(":"))(1))
                            If IsNumeric(TagId) Then
                                DataProvider.Instance.Tags_Delete(PortalId, ModuleId, TagId)
                            End If
                        Case "SAVE"
                            Dim sParams() As String = e.Parameters(4).Split(CChar(":"))
                            Dim TagName As String = sParams(1).Trim
                            Dim TagId As Integer = 0
                            Dim ForumId As Integer = -1
                            Dim ForumGroupId As Integer = -1
                            If sParams.Length > 2 Then
                                TagId = CInt(sParams(2))
                            End If
                            If sParams(3).Contains("FORUM") Then
                                ForumId = CInt(sParams(3).Replace("FORUM", String.Empty))
                            End If
                            If sParams(3).Contains("GROUP") Then
                                ForumGroupId = CInt(sParams(3).Replace("GROUP", String.Empty))
                            End If

                            If Not TagName = String.Empty Then
                                DataProvider.Instance.Tags_Save(PortalId, ModuleId, TagId, TagName, 0, 0, 0, -1, True, ForumId, ForumGroupId)
                            End If



                    End Select

                End If
                agCategories.DefaultParams = String.Empty
                Dim PageIndex As Integer = CInt(e.Parameters(0))
                Dim PageSize As Integer = CInt(e.Parameters(1))
                Dim SortColumn As String = e.Parameters(2).ToString
                Dim Sort As String = e.Parameters(3).ToString
                agCategories.Datasource = DataProvider.Instance.Tags_List(PortalId, ModuleId, True, PageIndex, PageSize, Sort, SortColumn, -1, -1)
                agCategories.Refresh(e.Output)
            Catch ex As Exception

            End Try
        End Sub
        Private Sub BindGroups()
            drpForums.Items.Add(New ListItem(Utilities.GetSharedResource("DropDownSelect"), "-1"))
            Dim fdb As New Data.ForumsDB
            Dim allForums As ForumCollection = fdb.Forums_List(PortalId, ModuleId)
            Dim filteredForums As New ForumCollection
            For Each f As Forum In allForums
                If f.ForumGroup.Active And f.Active And f.ParentForumId = 0 Then
                    f.TabId = TabId
                    f.SubForums = GetSubForums(allForums, f.ForumID)
                    filteredForums.Add(f)
                End If
            Next
            Dim tmpGroupId As Integer = -1
            For Each f As Forum In filteredForums
                If Not tmpGroupId = f.ForumGroupId Then
                    drpForums.Items.Add(New ListItem(f.GroupName, "GROUP" & f.ForumGroupId.ToString))
                    tmpGroupId = f.ForumGroupId
                End If
                drpForums.Items.Add(New ListItem(" - " & f.ForumName, "FORUM" & f.ForumID.ToString))
                If Not f.SubForums Is Nothing AndAlso f.SubForums.Count > 0 Then
                    For Each ff As Forum In f.SubForums
                        drpForums.Items.Add(New ListItem(" ---- " & ff.ForumName, "FORUM" & ff.ForumID.ToString))
                    Next
                End If
            Next
            'Dim dr As IDataReader = DataProvider.Instance.Forums_List(PortalId, ModuleId, -1, -1, False)


            'While dr.Read
            '    If Not tmpGroupId = CInt(dr("ForumGroupId")) Then
            '        drpForums.Items.Add(New ListItem(dr("GroupName").ToString, "GROUP" & dr("ForumGroupId").ToString))
            '        tmpGroupId = CInt(dr("ForumGroupId"))
            '    End If
            '    If Not CInt(dr("ForumId")) = 0 Then
            '        If CInt(dr("ParentForumID")) = 0 Then
            '            drpForums.Items.Add(New ListItem(" - " & dr("ForumName").ToString, "FORUM" & dr("ForumId").ToString))
            '        End If
            '        'If CInt(dr("ParentForumID")) > 0 Then
            '        '    drpForums.Items.Add(New ListItem(" ---- " & dr("ForumName").ToString, "FORUM" & dr("ForumId").ToString))
            '        'End If
            '    End If
            'End While
            'dr.Close()
        End Sub
        Private Sub agCategories_ItemBound(ByVal sender As Object, ByVal e As Modules.ActiveForums.Controls.ItemBoundEventArgs) Handles agCategories.ItemBound
            'e.Item(1) = Server.HtmlEncode(e.Item(1).ToString)
            'e.Item(2) = Server.HtmlEncode(e.Item(2).ToString)
            e.Item(6) = "<img src=""" & Page.ResolveUrl("~/desktopmodules/activeforums/images/delete16.png") & """ alt=""" & GetSharedResource("[RESX:Delete]") & """ height=""16"" width=""16"" />"
        End Sub
        Private Function GetSubForums(ByVal forums As ForumCollection, ByVal forumId As Integer) As ForumCollection
            Dim subforums As ForumCollection = Nothing
            For Each s As Forum In forums
                If s.ParentForumId = forumId Then
                    If subforums Is Nothing Then
                        subforums = New ForumCollection
                    End If
                    s.TabId = TabId
                    subforums.Add(s)
                End If
            Next
            Return subforums
        End Function
    End Class
End Namespace
